import sys
import tensorflow  as tf
import word2vct 
MAX_CONVERTER_SIZE=1024

print('\n在执行前请确认已经组建好模型(执行model_build.py)并且已保存\n是否继续?(Y/N)',end='')
if not input() == 'Y':
    sys.exit(0)

print('重载模组及词向量词典中.....')
converter=word2vct.Converter(MAX_CONVERTER_SIZE)
with open('./wordvct.json','r') as f:
    converter.loads(f.read())
    f.close()

model = tf.keras.models.load_model(r'saved_model')
print('进入模型测试模式，输入exit或Ctrl-C退出')
while True:
    try:
        text=input('测试文本:')
    except KeyboardInterrupt:
        print('Ctrl-C输入，退出')
        sys.exit(0)
    except EOFError:
        sys.exit(1)
    if text == '':
        continue
    elif text == 'exit':
        sys.exit(0)
    vct=converter.s2v(text)
    pre=model.predict(vct)
    result=0.0
    for i in pre:
        result+=i
    result/=len(pre)
    print('结果:%s'%result[0])
    if result <= 0.35:
        print('此评论为消极评论.')
    elif result >= 0.65:
        print('此评论为积极评论')
    else:
        print('此评论为中立评论.')
